/*
* CSET 3600
* Spring Semester 2013
* Melissa Brown
* Josh Siebenaler
* Matt Rich
* Christian Angelo
*
*/
package mancala;


public class HolePlacementAlgorithm {
    
    
    int [] HoleValues = new int [16];
    
    public static int [] ResetBoard(int [] ReturnHoleValues ){
        
        for(int i = 0; i < ReturnHoleValues.length; i ++){
            ReturnHoleValues[i] = 7;
        }
        return ReturnHoleValues;
        
        
    }
    
    public static int [] NextHoleComputation(int [] StonesInHoles, int currenthole){
       
        int stonesonboard = 0;
        int stonesleft = 0;
        int stonesright = 0; 
        int nexthole;
        
        if(currenthole < 0){
            
            currenthole = 1;
        }
        
        if(currenthole > 15){
            
            currenthole = 1;
        }
        
        nexthole = currenthole + 1;
        System.out.println("nexthole v2 is" + nexthole);
        
        while(StonesInHoles[currenthole] > 0){
            
            nexthole = LastHoleParity(StonesInHoles, currenthole,nexthole);
        }
        
        Capture(StonesInHoles, nexthole);
        
        
        for(int i = 1; i < 8; i ++){
            stonesonboard += StonesInHoles[i]; 
            stonesright += StonesInHoles[i];
        }
        
        for(int i = 9; i < StonesInHoles.length; i ++){
            stonesonboard += StonesInHoles[i];
            stonesleft += StonesInHoles[i];
        }
        
        if(stonesonboard == 0){
            
            StonesInHoles[1] = -2;
            MancalaGameBoardGUI.DisplayEndScreen(true);  
            
        }
        
        if(stonesleft == 0){
            MancalaGameBoardGUI.StatusGame = "left";
            MancalaGameBoardGUI.DisplayEndScreen(true);  
            
        }
        if(stonesright == 0){
            MancalaGameBoardGUI.StatusGame = "right";
            MancalaGameBoardGUI.DisplayEndScreen(true);  
        }
        
        return StonesInHoles;
    }
    
    
    public static int LastHoleParity(int [] StonesInHoles, int currenthole,int nexthole){
        
        
            
        nexthole = NextHolePlacement(StonesInHoles,currenthole,nexthole);
     
        return nexthole;
    }
    
    public static int NextHolePlacement(int [] StonesInHoles, int currenthole,int nexthole){
        
       

         if (nexthole > 15){
            
            StonesInHoles[0] +=1;
            StonesInHoles[currenthole] -=1;
            nexthole = 1;
         }else{
        
            StonesInHoles[nexthole] +=1;
            StonesInHoles[currenthole] -=1;
            nexthole +=1;
         }
      
         
        
        return nexthole;
    }
    
    public static int[] Capture(int [] StonesInHoles,int nexthole){
        int lasthole;
        if(nexthole > 15 || nexthole < 0){
            nexthole = 1;
        }
        
        System.out.println("nexthole is" + nexthole);
        lasthole = nexthole - 1;
        int pitvalue;
         boolean CaptureStatus;
        
     
        
      if(StonesInHoles[lasthole] == 1){
           // System.out.println("Last hole" + StonesInHoles[lasthole]);
          HardAI.Capture = true;
           if(lasthole >=9){
               pitvalue = 0;
               PlaceCaptureStones(StonesInHoles,pitvalue,lasthole);
           } 
           else{
               pitvalue = 8;
               PlaceCaptureStones(StonesInHoles,pitvalue,lasthole);
           }
           
            
        }
        
        
       return StonesInHoles;
    }
    
    public static int[] PlaceCaptureStones(int [] StonesInHoles, int pitvalue,int lasthole){
        
        if(lasthole == 15 || lasthole ==1){
                StonesInHoles[pitvalue] += StonesInHoles[15] + StonesInHoles[1];
                StonesInHoles[15] = 0;
                StonesInHoles[1] = 0;
            }
        if(lasthole == 14 || lasthole ==2){
                StonesInHoles[pitvalue] += StonesInHoles[14] + StonesInHoles[2];
                StonesInHoles[14] = 0;
                StonesInHoles[2] = 0;
            }
        if(lasthole == 13 || lasthole ==3){
                StonesInHoles[pitvalue] += StonesInHoles[13] + StonesInHoles[3];
                StonesInHoles[13] = 0;
                StonesInHoles[3] = 0;
            }
        if(lasthole == 12 || lasthole ==4){
                StonesInHoles[pitvalue] += StonesInHoles[12] + StonesInHoles[4];
                StonesInHoles[12] = 0;
                StonesInHoles[4] = 0;
           }   
        if(lasthole == 11 || lasthole ==5){
                StonesInHoles[pitvalue] += StonesInHoles[11] + StonesInHoles[5];
                StonesInHoles[11] = 0;
                StonesInHoles[5] = 0;
            }
        if(lasthole == 10 || lasthole ==6){
                StonesInHoles[pitvalue] += StonesInHoles[10] + StonesInHoles[6];
                StonesInHoles[10] = 0;
                StonesInHoles[6] = 0;
            }
        if(lasthole == 9 || lasthole ==7){
                StonesInHoles[pitvalue] += StonesInHoles[9] + StonesInHoles[7];
                StonesInHoles[9] = 0;
                StonesInHoles[7] = 0;
            }
           
            return StonesInHoles;
    }
   public static int[] ReturnHoleValue(int currenthole, int [] ReturnHoleValues)
           
   {
       int [] SetHoleValues = new int [16];
       int [] InternalArray = new int [16];
       int nexthole = 0;
      
            if(currenthole == 8){
                
                currenthole = 9;
            }
             
             
            
            if(currenthole > 15 || currenthole == 0){
                currenthole = 1;
            }
            
           System.out.println("currenthole is " + currenthole);
       
       if(currenthole == -1){
            for(int i = 1; i < 8; i ++){
                
               ReturnHoleValues[i] = 7;
            }
        
            
            for(int i = 9; i < ReturnHoleValues.length; i++){
                ReturnHoleValues[i] = 7;
               // System.out.println("left side values are" + ReturnHoleValues[i]);
            }
            
            ReturnHoleValues[0] = 0;
            ReturnHoleValues[8] = 0;
            
       }
       else{
           
           for(int i = 0; i < ReturnHoleValues.length; i ++){
               InternalArray[i] = ReturnHoleValues[i];
           }
       
            NextHoleComputation(InternalArray, currenthole);
          
           
            for (int i = 0; i <InternalArray.length; i ++){
                   ReturnHoleValues[i] = InternalArray[i];
            }   
           
       }
       return ReturnHoleValues;
       
   }

}
